package cn.sciento.farm.devicemanager.gateway.downlink.service.impl;


import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttSendGateway;
import cn.sciento.farm.devicemanager.gateway.downlink.service.SendService;
import cn.sciento.farm.devicemanager.gateway.model.downlink.MqttContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

/**
 * User: wumu
 * Date: 1/17/18
 * Time: 5:39 PM
 * @author wumu
 */

@Service
public class MqttSendService implements SendService {

    Logger logger = LoggerFactory.getLogger(MqttSendService.class);

    @Autowired
    private MqttSendGateway dataSendGateway;


    @Override
    public void send(MqttContent mqttContent){
        logger.info("mqtt消息发送完毕!!!!!!!" + mqttContent);
        String topic = mqttContent.getTopicPrefix()+mqttContent.getTopic();
        Message<String> message = MessageBuilder.withPayload(mqttContent.getPayload())
                .setHeader(MqttHeaders.TOPIC,topic)
                .build();
        dataSendGateway.sendToMqtt(message);
        logger.info("mqtt消息发送完毕!!!!!!!" + "topic:" + topic + " msg:" + message);
    }

}
